package base;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import base.check.Check;

public class XmlUtils {

	public static void check(Element element, String checkedName)  {
		Check.isTrue(checkedName.equals(element.getName()), Check.raiseException, "Unknown element " + element.getName());
	}
	
	static class File{
		public static Document read(InputStream inputStream) {
			SAXBuilder builder = new SAXBuilder();
			
			try {
				return builder.build(inputStream);
			} catch (Exception e) {
				return null;
			} 
			
		}
		
		public static void write(Document doc, OutputStream outStream) throws FileNotFoundException, IOException {
			XMLOutputter outputter = new XMLOutputter();
			outputter.setFormat(Format.getPrettyFormat());
			outputter.output(doc, outStream);
		}
	}
	
}
